﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace WPS.Modules.StyleLibrary
{
	public partial class Examples : System.Web.UI.Page
	{
		#region Template variables
		
		/// <summary>
		/// Javascript code to initialize one of the 'tables', 'images' or 'textboxes' array.
		/// </summary>
		protected string ExampleArray;

		/// <summary>
		/// Overview tab name to select on cancel.
		/// </summary>
		protected string SelectedPanel;

		#endregion

		#region Private variables

		/// <summary>Lists of current class names from wysiwyg.css</summary>
		private StyleNameList ClassNames;

		#endregion

		#region Page events

		protected void Page_Load(object sender, EventArgs e)
		{
			// Authenticate user
			IStyleLibraryIntegration cms = new StyleLibraryIntegration();
			if(!cms.CheckAccess())
			{
				Response.Redirect(cms.GetLoginPage(), true);
			}

			this.ClassNames = StyleSheetManager.LoadAllStyles(
				Server.MapPath(cms.GetExampleCssWebPath()));

			switch(Request.QueryString["styles"])
			{
				case "tables":
					MakeArray("tables", this.ClassNames.Tables);
					break;
				case "textboxes":
					MakeArray("textboxes", this.ClassNames.TextBoxes);
					break;
				case "images":
					MakeArray("images", this.ClassNames.Images);
					break;
			}
		}

		/// <summary>
		/// Make a javascript array from the class names for the client side scripts.
		/// </summary>
		/// <param name="variable">Output variable name in JS.</param>
		/// <param name="classNames">Style names to add to the generated array.</param>
		private void MakeArray(string variable, List<string> classNames)
		{
			this.ExampleArray = "";
			foreach(string style in classNames)
			{
				if(this.ExampleArray != "") { this.ExampleArray += ", "; }
				this.ExampleArray += "'" + style + "'";
			}

			if(this.ExampleArray != "")
			{
				this.ExampleArray = variable + " = new Array(" + this.ExampleArray + ");\r\n";
			}

			this.SelectedPanel = variable;
		}

		#endregion
	}
}